﻿package com.zhuangyzh.cmpp;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class CMPPManager {

    private static boolean connectedISMG = false;
    // 网关IP
    private static String serverIp;
    // 网关端口
    private static int serverPort;
    // 服务代码
    private static String serviceCode;
    // 企业代码
    private static String companyCode;
    // 业务代码
    private static String businessCode;
    // 密码
    private static String pwd;

    // 初始化需要配置的参
    public static String getServerIp() {
        return serverIp;
    }

    public static void setServerIp(String serverIp) {
        CMPPManager.serverIp = serverIp;
    }

    public static int getServerPort() {
        return serverPort;
    }

    public static void setServerPort(int serverPort) {
        CMPPManager.serverPort = serverPort;
    }

    public static String getServiceCode() {
        return serviceCode;
    }

    public static void setServiceCode(String serviceCode) {
        CMPPManager.serviceCode = serviceCode;
    }

    public static String getCompanyCode() {
        return companyCode;
    }

    public static void setCompanyCode(String companyCode) {
        CMPPManager.companyCode = companyCode;
    }

    public static String getBusinessCode() {
        return businessCode;
    }

    public static void setBusinessCode(String businessCode) {
        CMPPManager.businessCode = businessCode;
    }

    public static String getPwd() {
        return pwd;
    }

    public static void setPwd(String pwd) {
        CMPPManager.pwd = pwd;
    }

    public static boolean isConnectedISMG() {
        return connectedISMG;
    }

    public static void setConnectedISMG(boolean connectedISMG) {
        CMPPManager.connectedISMG = connectedISMG;
    }

    // ///////////////////////////////////////////////////////////////
    // /////////////////// SERVICE ////////////////////
    // ///////////////////////////////////////////////////////////////
    /*
     * 初始化配置参数
     */

    public static int InitConfig() {
        /**
         * 对端网管中心短信服务器地址：211.137.79.199 端口:8656 服务代码: 106571183013 企业代码: 417393
         * 业务代码:AHB0090102 密码为:Zxm10@@@
         */
        String classPath = System.getProperty("user.dir");
        // System.out.println("classpaht:" + classPath);
        String filepath = classPath + "\\bin\\cmpp.xml";
        // System.out.println("classpaht:" + filepath);
        try {
            // 1、创建一个DocumentBuilderFactory的对象 创建DocumentBuilder对象
            DocumentBuilder db = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();
            /* 注意导入Document对象时，要导入org.w3c.dom.Document包下的 */
            File file = new File(filepath);
            Document document = db.parse(file);
            Element root = document.getDocumentElement();
            NodeList nodeList = root.getChildNodes();
            if (null != root) {
                for (int i = 0; i < nodeList.getLength(); i++) {
                    Node node = nodeList.item(i);
                    if (node.getNodeType() == Node.ELEMENT_NODE) {
                        if ("ip".equals(node.getNodeName())) {
                            // System.out.println(node.getFirstChild().getNodeValue());
                            serverIp = new String(node.getFirstChild()
                                    .getNodeValue());
                            setServerIp(serverIp);
                        }
                        if ("port".equals(node.getNodeName())) {
                            // System.out.println(node.getFirstChild().getNodeValue());
                            serverPort = Integer.parseInt(node.getFirstChild()
                                    .getNodeValue());
                            setServerPort(serverPort);
                        }
                        if ("servicecode".equals(node.getNodeName())) {
                            // System.out.println(node.getFirstChild().getNodeValue());
                            serviceCode = new String(node.getFirstChild()
                                    .getNodeValue());
                            setServiceCode(serviceCode);

                        }
                        if ("companycode".equals(node.getNodeName())) {
                            // System.out.println(node.getFirstChild().getNodeValue());
                            companyCode = new String(node.getFirstChild()
                                    .getNodeValue());
                            setCompanyCode(companyCode);

                        }
                        if ("businesscode".equals(node.getNodeName())) {
                            // System.out.println(node.getFirstChild().getNodeValue());
                            businessCode = new String(node.getFirstChild()
                                    .getNodeValue());
                            setBusinessCode(businessCode);
                        }
                        if ("pwd".equals(node.getNodeName())) {
                            // System.out.println(node.getFirstChild().getNodeValue());
                            pwd = new String(node.getFirstChild()
                                    .getNodeValue());
                            setPwd(pwd);
                        }
                    }
                }
            }
            return 0;
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 发起认证注册请求
     * 
     * @return
     */
    public static int startConnect() {
        if (CMPPContainer.getSocketInstance() != null) {
            return CMPPContainer.connectISMG();
        }
        System.out.println("getSocketInstance error");
        return -1;
    }

    /**
     * 向网关提交短信
     * 
     * @return
     */
    public static int submitMessage(String msg, String number) {
        int ret = -1;
        if (!connectedISMG) {
            // 初始化配置参数
            CMPPManager.InitConfig();
            // 认证
            ret = CMPPManager.startConnect();
            if (ret != 0) {
                System.out.println("connnet to ISMG failed");
                connectedISMG = false;
                return ret;
            } else {
                // 认证通过
                connectedISMG = true;
                // 维持链路发送activetest
                CMPPService obj1 = new CMPPService();
                obj1.start();
            }
        }
        return CMPPContainer.sendShortMsg(msg, number);
    }

    public static void dealwithMessage() {
        CMPPContainer.dealwithISMG();
    }
    public static int connectMPIAG() {
        int ret = -1;
        if (!connectedISMG) {
            // 初始化配置参数
            CMPPManager.InitConfig();
            // 认证
            ret = CMPPManager.startConnect();
            if (ret != 0) {
                System.out.println("connnet to ISMG failed");
                connectedISMG = false;
                return ret;
            } else {
                // 认证通过
                connectedISMG = true;
                // 维持链路发送activetest
                CMPPService obj1 = new CMPPService();
                obj1.start();
            }
        }
        return 0;
    }
}
